package com.suoluo.zenadmin.controllers.admin.news;

import java.util.List;

import javax.annotation.Resource;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;

import com.suoluo.zenadmin.common.CommonResp;
import com.suoluo.zenadmin.common.JSONPageData;
import com.suoluo.zenadmin.common.PageUtil;
import com.suoluo.zenadmin.common.SystemType;
import com.suoluo.zenadmin.controllers.JSONInterceptor.JSON;
import com.suoluo.zenadmin.service.InfoService;
import com.suoluo.zenadmin.service.news.NewsService;
import com.suoluo.zenadmin.vo.BaseInfoVO;
import com.suoluo.zenadmin.vo.ImageVO;
import com.suoluo.zenadmin.vo.PageVO;
import com.suoluo.zenadmin.vo.news.News2VO;

@Path("/")
public class NewsController {

	@Resource(name="newsService")
	private NewsService newsService;
	
	@Resource(name="infoService")
	private InfoService infoService;
	
	@Get("vlist")
	public String toListNews()
	{
		return "listNews";
	}
	@JSON
	@Post("list")
	public Object listNews(PageVO page)
	{
		int curr = page.getPage();
		int rows = page.getRows();
		int start = PageUtil.calcStartRow(curr, rows);
		JSONPageData data = new JSONPageData();
		List<News2VO> list = newsService.findSimplestNews(start,rows);
		data.setRows(list);
		int total = newsService.findNewsCount();
		data.setTotal(total);
		return data;
	}
	@JSON
	@Post("type/list")
	public Object listNewsType()
	{
		return newsService.findListNewsType();
	}
	@Get("vsave")
	public String toAddNews()
	{
		return "addNews";
	}
	@JSON
	@Post("save")
	public Object saveNews(News2VO newsVO)
	{
		newsService.saveNews(newsVO);
		return CommonResp.SUCCESS;
	}
	@Get("vupdate")
	public String toUpdateNews(Invocation invo,@Param("newsId")int newsId)
	{
		News2VO news = newsService.findNewsById(newsId);
		invo.addModel("news",news);
		return "updateNews";
	}
	@JSON
	@Post("update")
	public Object updateNews(News2VO newsVO)
	{
		newsService.updateNews(newsVO);
		return CommonResp.SUCCESS;
	}
	@JSON
	@Post("delete")
	public Object deleteNews(News2VO newsVO)
	{
		newsService.deleteNews(newsVO);
		return CommonResp.SUCCESS;
	}
	@Get("text/vlist")
	public String toListNewsImages(Invocation inv,@Param("newsId")int newsId){
		inv.addModel("newsId",newsId);
		return "listNewsText";
	}
	@JSON
	@Post("text/list")
	public Object findSubNews(@Param("newsId")int newsId){
		
		return infoService.findBaseInfos(newsId, SystemType.INFO_NEWS); 
	}
	@Get("text/vsave")
	public String toAddNewsText(){
		return "addNewsText";
	}
	@JSON
	@Post("text/save")
	public Object saveNewsText(BaseInfoVO baseVO){
		
		newsService.saveNewsText(baseVO);
		return CommonResp.SUCCESS;
	}
	
	@Get("text/vupdate")
	public String toUpdateNewsText(Invocation inv,@Param("infoId")int infoId){
		BaseInfoVO base = infoService.findBaseInfoById(infoId);
		inv.addModel("text",base);
		return "updateNewsText";
	}
	@JSON
	@Post("text/update")
	public Object updateNewsText(BaseInfoVO baseVO){
		
		infoService.updateBaseInfo(baseVO);
		return CommonResp.SUCCESS;
	}
	@JSON
	@Post("text/delete")
	public Object deleteNewsText(BaseInfoVO baseVO){
		
		//删除基础信息
		infoService.deleteBaseInfoById(baseVO.getInfoId());
		return CommonResp.SUCCESS;
	}
	/**************************新闻图片管理**start*****************************/
	@Get("images/vlist")
	public String toListFeatureItemImages(Invocation inv,@Param("infoId")int infoId,@Param("newsId")int newsId) {
		inv.addModel("infoId",infoId);
		inv.addModel("newsId",newsId);
		return "listNewsImages";
	}
	@JSON
	@Post("images/list")
	public Object showNewsImages(@Param("infoId")int infoId) {
		return infoService.findImages(infoId);
	}
	@Get("images/vsave")
	public String toAddNewsImages() {
		return "addNewsImages";
	}
	@JSON
	@Post("images/save")
	public Object saveNewsImages(BaseInfoVO infoVO) {
		newsService.saveNewsImages(infoVO);
		return CommonResp.SUCCESS;
	}
	@Get("images/vupdate")
	public String toUpdateNewsImages(Invocation inv,@Param("imageId")int imageId) {
		ImageVO images = infoService.findImageById(imageId);
		inv.addModel("image",images);
		return "updateNewsImages";
	}
	@JSON
	@Post("images/update")
	public Object updateFeatureItemImage(ImageVO image) {
		infoService.updateImage(image);
		return CommonResp.SUCCESS;
	}
	@JSON
	@Post("images/delete")
	public Object deleteFeatureItemImage(ImageVO image) {
		infoService.deleteImage(image);
		return CommonResp.SUCCESS;
	}
	
	/**************************新闻图片管理**end*****************************/
}
